/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：ArticlesSortService.java
 * History:
 *       Apr 16, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.service.admin;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xteam.ecsystem.dao.IArticlesSortDao;

/**
 * 文章分类业务管理层
 * @author liuzhi
 *
 */
@Service
public class ArticlesSortService {
	
	@Autowired
	private IArticlesSortDao asDao;
	
	/**
	 * 
	 * 删除单条或多条文章分类信息
	 * @author liuzhi
	 * @param baiIds
	 */
	public void delObject(String baiIds){
		this.asDao.delObject(baiIds);
	}
	
	/**
	 * 
	 * 新增或者修改文章分类信息
	 * @author liuzhi
	 * @param obj
	 * @param bagId
	 */
	public void saveOrUpdateObject(Object obj, String bagId){
		this.asDao.saveOrUpdateObject(obj, bagId);
	}
	
	/**
	 * 
	 * 条件查询和分页显示文章分类信息（预留）
	 * @author liuzhi
	 * @param index
	 * @param size
	 * @param bagName
	 * @return
	 */
	public String getBasInfoList(int pageIndex, int pageSize, String bagName){
		return this.asDao.getBasInfoList(pageIndex, pageSize, bagName);
	}
	
	/**
	 * 根据文章分类编号获得对应的文章分类信息
	 * @author liuzhi
	 * @param bagId
	 * @return
	 */
	public String getBasInfoById(String bagId){
		return this.asDao.getBasInfoById(bagId);
	}
	
	/**
	 * 
	 * 根据文章分类编号修改文章分类状态
	 * 0：无效
	 * 1：有效
	 * @author liuzhi
	 * @param bagId
	 */
	public void updateBasStateById(String bagId){
		this.asDao.updateBasStateById(bagId);
	}
	
	/**
	 * 获得文章分类TreeGrid
	 * @author liuzhi
	 * @param request
	 * @param pageIndex
	 * @param pageSize
	 * @param bagName
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getBasTreeGrid(HttpServletRequest request,
			int pageIndex, int pageSize, String bagName){
		JSONArray jsonArray = this.asDao.getBasJsonArray(null, null);
		
		PluSoft.Data.DataTree tree = new PluSoft.Data.DataTree();
	    tree.IdField = "bagId";
	    tree.NodesField = "children";
	    tree.ParentField = "bagParent";
		tree.LoadExpandCollapseConfig(request);
		
		ArrayList list = (ArrayList)PluSoft.Utils.JSON.Decode(jsonArray.toString());
	    tree.LoadTree(list);
	    
	    //按分类名称模糊查询
	    if (bagName != null && bagName.length() > 0
				&& !bagName.equals("undefined")
				&& !bagName.equals("null")){
	        ArrayList filters = findNodesByCondition(bagName, tree.GetList());
	        tree.CreateDataView(filters);
	    }
	    
	    //用于存储总记录数和数据列表对象
	    Hashtable table = new Hashtable();
	    
	    //获取折叠、过滤、排序后的总记录数
	    int total = tree.GetCollapsedCount(); 
	    table.put("total", total);
	    
	    //从数据视图中获取分页数据
	    ArrayList data = tree.GetPagedNodes(pageIndex, pageSize);     
	    table.put("data", data);
	
	    String json = PluSoft.Utils.JSON.Encode(table);
	    return json;
	}
	
	/**
	 * 模糊查找节点信息
	 * @author liuzhi
	 * @param bggName
	 * @param nodeList
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private ArrayList findNodesByCondition(String bagName, ArrayList nodeList){
	    ArrayList filters = new ArrayList();

	    for (int i = 0, l = nodeList.size(); i < l; i++){
	        Map node = (Map)nodeList.get(i);
	        String condition = node.get("bagName").toString();
	        
	        if (condition != null && condition.indexOf(bagName) != -1){
	            filters.add(node);
	        }
	    }

	    return filters;
	}
	
	/**
	 * 懒加载文章分类树
	 * @author liuzhi
	 * @param bagParent
	 * @return
	 */
	public String lazyLoadBasTree(String bagParent){
		return this.asDao.lazyLoadBasTree(bagParent);
	}
	
	/**
	 * 懒加载文章分类树形表格
	 * @author liuzhi
	 * @param bagParent
	 * @return
	 */
	public String lazyLoadBasTreeGrid(String bagParent){
		return this.asDao.lazyLoadBasTreeGrid(bagParent);
	}

	/**
	 * 检验选择的文章分类是否已被文章列表所使用
	 * @author liuzhi Oct 18, 2013
	 * @param bagIds
	 * @return
	 */
	public boolean getBaiByBas(String bagIds) {
		return this.asDao.getBaiByBas(bagIds);
	}
}
